package cn.lyq.card.ui.qualified

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.lyq.card.R
import cn.lyq.card.data.model.CashBackStageWrapInfo
import cn.lyq.card.utils.setClick


class CashBackStageAdapter(
    private val context: Context,
    val data: MutableList<CashBackStageWrapInfo>
) :
    RecyclerView.Adapter<CashBackStageAdapter.VH>() {

    var onClick: OnClick? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        return VH(
            LayoutInflater.from(context).inflate(R.layout.item_qualified_device, parent, false)
        )
    }

    override fun getItemCount() = data.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.tvType.text = data[position].title
        holder.tvStageDesc.text = data[position].getStageStr()
        holder.subAdapter.setList(data[position].list)
    }

    interface OnClick {
        fun click(position: Int, levelIndex: Int)
    }

    inner class VH(root: View) : RecyclerView.ViewHolder(root) {

        val tvType: TextView = root.findViewById(R.id.tvType)
        val tvStageDesc: TextView = root.findViewById(R.id.tvStageDesc)
        private val rvStage: RecyclerView = root.findViewById(R.id.rvStage)
        private val itemQualifiedDevice: View = root.findViewById(R.id.itemQualifiedDevice)
        val subAdapter = CashBackStageSubAdapter()

        init {
            setClick(itemQualifiedDevice) {
                onClick?.click(adapterPosition, 0)
            }
            subAdapter.setOnItemClickListener { _, _, position ->
                onClick?.click(adapterPosition, position)
            }
            rvStage.layoutManager = GridLayoutManager(context, 2)
            rvStage.adapter = subAdapter

        }
    }
}